<?php

namespace app\modules\backend\controllers;

use app\models\Setting;
use app\models\User;
use app\models\UserReturnRecord;
use app\models\CashOutRecord;
use app\tools\Re;
use Yii;

class ReturnController extends BaseController
{
    /**
     * @param string $type
     * @return array
     */
    public function actionSet(){
        $register_return = Yii::$app->request->post('register_return', 1);
        $register_return_money = Yii::$app->request->post('register_return_money', 0.00);
        $order_return = Yii::$app->request->post('order_return', 1);
        if (!is_numeric($register_return_money)) {
            return Re::data(null, '返现金额格式错误', Re::ERROR);
        }
        $setting = Setting::findOne(1);
        $setting->register_return = $register_return;
        $setting->register_return_money = $register_return_money;
        $setting->order_return = $order_return;
        $setting->save();
        return Re::data();
    }

    public function actionGetSettingInfo(){
        $setting = Setting::findOne(1);
        return Re::data(['data' => $setting]);
    }

    public function actionUserList(){
        $page = Yii::$app->request->post('page', 0);
        $nickname = Yii::$app->request->post('nickname', '');
        $phone = Yii::$app->request->post('phone', '');
        $user_query = UserReturnRecord::find()
            ->leftJoin('user', 'user_return_record.user_id = user.id')
            ->where(['user_return_record.status' => 1]);
        if ($nickname) {
            $user_query->andWhere(['like', 'nickname', $nickname]);
        }
        if ($phone) {
            $user_query->andWhere(['like', 'phone', $phone]);
        }
        $user_id_arr = $user_query->select("user_id")->groupBy("user_id")->limit(UserReturnRecord::PAGE_SIZE)->offset(UserReturnRecord::PAGE_SIZE*$page)->column();
        $data = [];
        if ($user_id_arr) {
            foreach ($user_id_arr as $v) {
                $user = User::findOne($v);
                $user_id = $v;
                $timestemp = strtotime('-25 days');
                //echo $timestemp;die;
                $cash_out_all = CashOutRecord::find()->where(['user_id' => $user_id, 'status' => [0,1]])->SUM('money');
                $cash_out_all = is_numeric($cash_out_all) ? $cash_out_all : 0;
                $profit_all = UserReturnRecord::find()->where(['user_id' => $user_id, 'status' => 1])->SUM('money');
                $profit_all = is_numeric($profit_all) ? $profit_all : 0;
                $profit_ky_zc = UserReturnRecord::find()->where(['user_id' => $user_id, 'type' => 1, 'status' => 1])->SUM('money');
                $profit_order = UserReturnRecord::find()->where(['user_id' => $user_id, 'type' => 2, 'status' => 1])->SUM('money');
                $profit_ky_order = UserReturnRecord::find()->where(['user_id' => $user_id, 'type' => 2, 'status' => 1])->andWhere(['<=', 'created_at', $timestemp])->SUM('money');
                $profit_ky_zc = is_numeric($profit_ky_zc) ? $profit_ky_zc : 0;
                $profit_order = is_numeric($profit_order) ? $profit_order : 0;
                $profit_ky_order = is_numeric($profit_ky_order) ? $profit_ky_order : 0;
                $profit_ky = bcadd($profit_ky_zc, $profit_ky_order, 2);
                $profit_yjs = $profit_ky;
                $profit_ky = bcsub($profit_ky, $cash_out_all, 2);
                $profit_djs = UserReturnRecord::find()->where(['user_id' => $user_id, 'type' => 2, 'status' => 1])->andWhere(['>=', 'created_at', $timestemp])->SUM('money');
                $profit_djs = is_numeric($profit_djs) ? $profit_djs : 0;
                $data[] = [
                    'nickname' => $user->nickname,
                    'phone' => $user->phone,
                    'return_money_zc' => $profit_ky_zc,
                    'return_money_order' => $profit_order,
                    'return_money_all' => $profit_all,
                    'cash_out_all' => $cash_out_all,
                    'return_money_djs' => $profit_djs,
                    'return_money_yjs' => $profit_yjs,
                ];
            }
        }
        return Re::data([
            'data' => $data,
            'totalPage' => $user_query->count(),
            'currentPage' => $page,
            'perPage' => UserReturnRecord::PAGE_SIZE
        ]);
    }

    public function actionCashList(){
        $page = Yii::$app->request->post('page', 0);
        $nickname = Yii::$app->request->post('nickname', '');
        $phone = Yii::$app->request->post('phone', '');
        $status = Yii::$app->request->post('status', '-2');
        $type = Yii::$app->request->post('type', -1);
        $account = Yii::$app->request->post('account', '');
        $date_range = Yii::$app->request->post('created_at_range', '');
        $query = CashOutRecord::find()
            ->leftJoin('user', 'cash_out_record.user_id = user.id')
            ->where(['cash_out_record.status' => [0,1]]);
        if ($nickname) {
            $query->andWhere(['like', 'nickname', $nickname]);
        }
        if ($phone) {
            $query->andWhere(['like', 'phone', $phone]);
        }
        if ($type > 0) {
            $query->andWhere(['type' => $type]);
        }
        if ($status > -2) {
            if ($status == -1) {
              $query->andWhere(['cash_out_record.status' => [0,1]]);
            } else {
              $query->andWhere(['cash_out_record.status' => $status]);
            }
        }
        if ($account) {
            $query->andWhere(['like', 'account', $account]);
        }
        if ($date_range) {
          $data_arr = explode("-", $date_range);
          $start_at = $data_arr[0];
          $end_at = $data_arr[1];
          $query->andWhere("cash_out_record.created_at >= '$start_at' and cash_out_record.created_at <= '$end_at'");
        }
        $cash_list = $query->orderBy("status asc, created_at desc")->limit(CashOutRecord::PAGE_SIZE)->offset(CashOutRecord::PAGE_SIZE*$page)->all();
        $cash_data = [];
        if ($cash_list) {
            foreach ($cash_list as $v) {
                $cash_data[] = [
                    'id' => $v->id,
                    'nickname' => $v->user->nickname,
                    'phone' => $v->user->phone,
                    'name' => $v->name,
                    'card' => $v->card,
                    'account' => $v->account,
                    'type' => $v->type,
                    'type_name' => $v->type == 1 ? '微信' : '支付宝',
                    'money' => $v->money,
                    'status' => $v->status,
                    'status_name' => $v->status == 0 ? '待提现' : ($v->status == 1 ? '提现完成' : '提现先失败'),
                    'created_time' => date("Y-m-d H:i:s", $v->created_at)
                ];
            }
        }
        return Re::data([
            'data' => $cash_data,
            'totalPage' => $query->count(),
            'currentPage' => $page,
            'perPage' => CashOutRecord::PAGE_SIZE
        ]);
    }

    public function actionFinshCash(){
        $id = Yii::$app->request->post('id', 0);
        $cashInfo = CashOutRecord::findOne($id);
        if (empty($cashInfo)) {
          return Re::data(null, '提现信息读取错误', Re::ERROR);
        }
        $cashInfo->status = 1;
        if (!$cashInfo->save()) {
          return Re::data(null, '提现失败', Re::ERROR);
        }
        return Re::data();
    }

    public function actionUnFinshCashCount(){
      $count = CashOutRecord::find()->where(['status' => 0])->count();
      return Re::data(['count' => $count]);
    }
}
